package cc.orange.mainView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.luck.picture.lib.adapter.holder.BasePreviewHolder;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.manager.SelectedManager;
import com.luck.picture.lib.utils.ActivityCompatHelper;
import com.luck.picture.lib.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;
import cc.orange.R;
import cc.orange.adapter.PiPreviewAdapter;
import cc.orange.databinding.ActivityAlbumPreviewBinding;
import cc.orange.utils.ImgDownLoadUtils;
import cc.orange.utils.RequestPermissions;
import cc.orange.utils.ZXToastUtil;

public class AlbumPreviewActivity extends AppCompatActivity {

    private ActivityAlbumPreviewBinding binding;
    protected PiPreviewAdapter viewPageAdapter;
    private int curPosition;
    protected int screenWidth, screenHeight;
    private  ArrayList<LocalMedia> mdata = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_album_preview);
        List<String>list = getIntent().getStringArrayListExtra("p_media");
        for (int i = 0; i < list.size(); i++) {
            LocalMedia media = LocalMedia.generateHttpAsLocalMedia(list.get(0));
            mdata.add(media);
        }

        int albn_where = getIntent().getIntExtra("albn_where", 0);
        if(albn_where == 1){
            binding.albumRelDown.setVisibility(View.VISIBLE);
        }

        initViews(mdata);

    }

    private void initViews(ArrayList<LocalMedia> data) {
         RelativeLayout.LayoutParams pp = new RelativeLayout.
                LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                10);
        binding.albumTitlesss.setLayoutParams(pp);
        screenWidth = DensityUtil.getRealScreenWidth(this);
        screenHeight = DensityUtil.getScreenHeight(this);
        ViewPager2 viewPager = new ViewPager2(this);
        binding.magical.setMagicalContent(viewPager);

        viewPageAdapter = new PiPreviewAdapter();
        viewPageAdapter.setData(data);
        viewPageAdapter.setOnPreviewEventListener(new MyOnPreviewEventListener());
        viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        viewPager.setAdapter(viewPageAdapter);
        SelectedManager.clearPreviewData();
        if (data.size() == 0 || curPosition > data.size()) {
            finish();
            return;
        }
        LocalMedia media = data.get(curPosition);

        viewPager.registerOnPageChangeCallback(pageChangeCallback);
        viewPager.setPageTransformer(new MarginPageTransformer(DensityUtil.dip2px(this, 3)));
        viewPager.setCurrentItem(curPosition, false);

        binding.albumBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //下载图片
        binding.albumRelDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestPermissions.getPermissions_storage(AlbumPreviewActivity.this,
                        new RequestPermissions.SuccessInterf() {
                            @Override
                            public void onSuccess() {
                                ImgDownLoadUtils.getImagFile2(AlbumPreviewActivity.this,
                                        mdata.get(0).getPath(),
                                        ""+System.currentTimeMillis(),
                                        new ImgDownLoadUtils.SuccessListener() {
                                            @Override
                                            public void onSuccessDown(String se) {
                                                ZXToastUtil.showToast("下载成功");
                                            }

                                            @Override
                                            public void onFail(String se) {
                                                ZXToastUtil.showToast("下载失败");
                                            }
                                        });
                            }
                        });
            }
        });
    }

    private final ViewPager2.OnPageChangeCallback pageChangeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            curPosition = position;
            if (mdata.size() > position) {
                viewPageAdapter.setVideoPlayButtonUI(position);

            }
        }
    };

    /**
     * ViewPageAdapter回调事件处理
     */
    private class MyOnPreviewEventListener implements BasePreviewHolder.OnPreviewEventListener {

        @Override
        public void onBackPressed() {
//            binding.magical.backToMin();
        }

        @Override
        public void onPreviewVideoTitle(String videoName) {
//            if (TextUtils.isEmpty(videoName)) {
//                titleBar.setTitle((curPosition + 1) + "/" + totalNum);
//            } else {
//                titleBar.setTitle(videoName);
//            }
        }

        @Override
        public void onLongPressDownload(LocalMedia media) {
        }
    }

    /**
     * 回到初始位置
     */
    private void onKeyDownBackToMin() {
        if (!ActivityCompatHelper.isDestroy(this)) {
            binding.magical.backToMin();
        }
    }
}